home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / os2emxpath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  13.2 KB  |  421 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. '''Common pathname manipulations, OS/2 EMX version.
  5.  
  6. Instead of importing this module directly, import os and refer to this
  7. module as os.path.
  8. '''
  9. import os
  10. import stat
  11. __all__ = [
  12.     'normcase',
  13.     'isabs',
  14.     'join',
  15.     'splitdrive',
  16.     'split',
  17.     'splitext',
  18.     'basename',
  19.     'dirname',
  20.     'commonprefix',
  21.     'getsize',
  22.     'getmtime',
  23.     'getatime',
  24.     'getctime',
  25.     'islink',
  26.     'exists',
  27.     'isdir',
  28.     'isfile',
  29.     'ismount',
  30.     'walk',
  31.     'expanduser',
  32.     'expandvars',
  33.     'normpath',
  34.     'abspath',
  35.     'splitunc',
  36.     'curdir',
  37.     'pardir',
  38.     'sep',
  39.     'pathsep',
  40.     'defpath',
  41.     'altsep',
  42.     'extsep',
  43.     'realpath',
  44.     'supports_unicode_filenames']
  45. curdir = '.'
  46. pardir = '..'
  47. extsep = '.'
  48. sep = '/'
  49. altsep = '\\'
  50. pathsep = ';'
  51. defpath = '.;C:\\bin'
  52.  
  53. def normcase(s):
  54.     '''Normalize case of pathname.
  55.  
  56.     Makes all characters lowercase and all altseps into seps.'''
  57.     return s.replace('\\', '/').lower()
  58.  
  59.  
  60. def isabs(s):
  61.     '''Test whether a path is absolute'''
  62.     s = splitdrive(s)[1]
  63.     if s != '':
  64.         pass
  65.     return s[:1] in '/\\'
  66.  
  67.  
  68. def join(a, *p):
  69.     '''Join two or more pathname components, inserting sep as needed'''
  70.     path = a
  71.     for b in p:
  72.         if isabs(b):
  73.             path = b
  74.             continue
  75.         if path == '' or path[-1:] in '/\\:':
  76.             path = path + b
  77.             continue
  78.         path = path + '/' + b
  79.     
  80.     return path
  81.  
  82.  
  83. def splitdrive(p):
  84.     '''Split a pathname into drive and path specifiers. Returns a 2-tuple
  85. "(drive,path)";  either part may be empty'''
  86.     if p[1:2] == ':':
  87.         return (p[0:2], p[2:])
  88.     
  89.     return ('', p)
  90.  
  91.  
  92. def splitunc(p):
  93.     """Split a pathname into UNC mount point and relative path specifiers.
  94.  
  95.     Return a 2-tuple (unc, rest); either part may be empty.
  96.     If unc is not empty, it has the form '//host/mount' (or similar
  97.     using backslashes).  unc+rest is always the input path.
  98.     Paths containing drive letters never have an UNC part.
  99.     """
  100.     if p[1:2] == ':':
  101.         return ('', p)
  102.     
  103.     firstTwo = p[0:2]
  104.     if firstTwo == '/' * 2 or firstTwo == '\\' * 2:
  105.         normp = normcase(p)
  106.         index = normp.find('/', 2)
  107.         if index == -1:
  108.             return ('', p)
  109.         
  110.         index = normp.find('/', index + 1)
  111.         if index == -1:
  112.             index = len(p)
  113.         
  114.         return (p[:index], p[index:])
  115.     
  116.     return ('', p)
  117.  
  118.  
  119. def split(p):
  120.     '''Split a pathname.
  121.  
  122.     Return tuple (head, tail) where tail is everything after the final slash.
  123.     Either part may be empty.'''
  124.     (d, p) = splitdrive(p)
  125.     i = len(p)
  126.     while i and p[i - 1] not in '/\\':
  127.         i = i - 1
  128.     (head, tail) = (p[:i], p[i:])
  129.     head2 = head
  130.     while head2 and head2[-1] in '/\\':
  131.         head2 = head2[:-1]
  132.     if not head2:
  133.         pass
  134.     head = head
  135.     return (d + head, tail)
  136.  
  137.  
  138. def splitext(p):
  139.     '''Split the extension from a pathname.
  140.  
  141.     Extension is everything from the last dot to the end.
  142.     Return (root, ext), either part may be empty.'''
  143.     (root, ext) = ('', '')
  144.     for c in p:
  145.         if c in [
  146.             '/',
  147.             '\\']:
  148.             (root, ext) = (root + ext + c, '')
  149.             continue
  150.         if c == '.':
  151.             if ext:
  152.                 (root, ext) = (root + ext, c)
  153.             else:
  154.                 ext = c
  155.         ext
  156.         if ext:
  157.             ext = ext + c
  158.             continue
  159.         root = root + c
  160.     
  161.     return (root, ext)
  162.  
  163.  
  164. def basename(p):
  165.     '''Returns the final component of a pathname'''
  166.     return split(p)[1]
  167.  
  168.  
  169. def dirname(p):
  170.     '''Returns the directory component of a pathname'''
  171.     return split(p)[0]
  172.  
  173.  
  174. def commonprefix(m):
  175.     '''Given a list of pathnames, returns the longest common leading component'''
  176.     if not m:
  177.         return ''
  178.     
  179.     prefix = m[0]
  180.     for item in m:
  181.         for i in range(len(prefix)):
  182.             if prefix[:i + 1] != item[:i + 1]:
  183.                 prefix = prefix[:i]
  184.                 if i == 0:
  185.                     return ''
  186.                 
  187.                 break
  188.                 continue
  189.         
  190.     
  191.     return prefix
  192.  
  193.  
  194. def getsize(filename):
  195.     '''Return the size of a file, reported by os.stat()'''
  196.     return os.stat(filename).st_size
  197.  
  198.  
  199. def getmtime(filename):
  200.     '''Return the last modification time of a file, reported by os.stat()'''
  201.     return os.stat(filename).st_mtime
  202.  
  203.  
  204. def getatime(filename):
  205.     '''Return the last access time of a file, reported by os.stat()'''
  206.     return os.stat(filename).st_atime
  207.  
  208.  
  209. def getctime(filename):
  210.     '''Return the creation time of a file, reported by os.stat().'''
  211.     return os.stat(filename).st_ctime
  212.  
  213.  
  214. def islink(path):
  215.     '''Test for symbolic link.  On OS/2 always returns false'''
  216.     return False
  217.  
  218.  
  219. def exists(path):
  220.     '''Test whether a path exists'''
  221.     
  222.     try:
  223.         st = os.stat(path)
  224.     except os.error:
  225.         return False
  226.  
  227.     return True
  228.  
  229.  
  230. def isdir(path):
  231.     '''Test whether a path is a directory'''
  232.     
  233.     try:
  234.         st = os.stat(path)
  235.     except os.error:
  236.         return False
  237.  
  238.     return stat.S_ISDIR(st.st_mode)
  239.  
  240.  
  241. def isfile(path):
  242.     '''Test whether a path is a regular file'''
  243.     
  244.     try:
  245.         st = os.stat(path)
  246.     except os.error:
  247.         return False
  248.  
  249.     return stat.S_ISREG(st.st_mode)
  250.  
  251.  
  252. def ismount(path):
  253.     '''Test whether a path is a mount point (defined as root of drive)'''
  254.     (unc, rest) = splitunc(path)
  255.     if unc:
  256.         return rest in ('', '/', '\\')
  257.     
  258.     p = splitdrive(path)[1]
  259.     if len(p) == 1:
  260.         pass
  261.     return p[0] in '/\\'
  262.  
  263.  
  264. def walk(top, func, arg):
  265.     '''Directory tree walk whth callback function.
  266.  
  267.     walk(top, func, arg) calls func(arg, d, files) for each directory d
  268.     in the tree rooted at top (including top itself); files is a list
  269.     of all the files and subdirs in directory d.'''
  270.     
  271.     try:
  272.         names = os.listdir(top)
  273.     except os.error:
  274.         return None
  275.  
  276.     func(arg, top, names)
  277.     exceptions = ('.', '..')
  278.     for name in names:
  279.         if name not in exceptions:
  280.             name = join(top, name)
  281.             if isdir(name):
  282.                 walk(name, func, arg)
  283.             
  284.         isdir(name)
  285.     
  286.  
  287.  
  288. def expanduser(path):
  289.     '''Expand ~ and ~user constructs.
  290.  
  291.     If user or $HOME is unknown, do nothing.'''
  292.     if path[:1] != '~':
  293.         return path
  294.     
  295.     (i, n) = (1, len(path))
  296.     while i < n and path[i] not in '/\\':
  297.         i = i + 1
  298.     if i == 1:
  299.         if 'HOME' in os.environ:
  300.             userhome = os.environ['HOME']
  301.         elif not ('HOMEPATH' in os.environ):
  302.             return path
  303.         else:
  304.             
  305.             try:
  306.                 drive = os.environ['HOMEDRIVE']
  307.             except KeyError:
  308.                 drive = ''
  309.  
  310.             userhome = join(drive, os.environ['HOMEPATH'])
  311.     else:
  312.         return path
  313.     return userhome + path[i:]
  314.  
  315.  
  316. def expandvars(path):
  317.     '''Expand shell variables of form $var and ${var}.
  318.  
  319.     Unknown variables are left unchanged.'''
  320.     if '$' not in path:
  321.         return path
  322.     
  323.     import string
  324.     varchars = string.letters + string.digits + '_-'
  325.     res = ''
  326.     index = 0
  327.     pathlen = len(path)
  328.     while index < pathlen:
  329.         c = path[index]
  330.         if c == "'":
  331.             path = path[index + 1:]
  332.             pathlen = len(path)
  333.             
  334.             try:
  335.                 index = path.index("'")
  336.                 res = res + "'" + path[:index + 1]
  337.             except ValueError:
  338.                 res = res + path
  339.                 index = pathlen - 1
  340.             except:
  341.                 None<EXCEPTION MATCH>ValueError
  342.             
  343.  
  344.         None<EXCEPTION MATCH>ValueError
  345.         if c == '$':
  346.             if path[index + 1:index + 2] == '$':
  347.                 res = res + c
  348.                 index = index + 1
  349.             elif path[index + 1:index + 2] == '{':
  350.                 path = path[index + 2:]
  351.                 pathlen = len(path)
  352.                 
  353.                 try:
  354.                     index = path.index('}')
  355.                     var = path[:index]
  356.                     if var in os.environ:
  357.                         res = res + os.environ[var]
  358.                 except ValueError:
  359.                     res = res + path
  360.                     index = pathlen - 1
  361.                 except:
  362.                     None<EXCEPTION MATCH>ValueError
  363.                 
  364.  
  365.             None<EXCEPTION MATCH>ValueError
  366.             var = ''
  367.             index = index + 1
  368.             c = path[index:index + 1]
  369.             while c != '' and c in varchars:
  370.                 var = var + c
  371.                 index = index + 1
  372.                 c = path[index:index + 1]
  373.             if var in os.environ:
  374.                 res = res + os.environ[var]
  375.             
  376.             if c != '':
  377.                 res = res + c
  378.             
  379.         else:
  380.             res = res + c
  381.         index = index + 1
  382.     return res
  383.  
  384.  
  385. def normpath(path):
  386.     '''Normalize path, eliminating double slashes, etc.'''
  387.     path = path.replace('\\', '/')
  388.     (prefix, path) = splitdrive(path)
  389.     while path[:1] == '/':
  390.         prefix = prefix + '/'
  391.         path = path[1:]
  392.     comps = path.split('/')
  393.     i = 0
  394.     while i < len(comps):
  395.         if comps[i] == '.':
  396.             del comps[i]
  397.             continue
  398.         if comps[i] == '..' and i > 0 and comps[i - 1] not in ('', '..'):
  399.             del comps[i - 1:i + 1]
  400.             i = i - 1
  401.             continue
  402.         if comps[i] == '' and i > 0 and comps[i - 1] != '':
  403.             del comps[i]
  404.             continue
  405.         i = i + 1
  406.     if not prefix and not comps:
  407.         comps.append('.')
  408.     
  409.     return prefix + '/'.join(comps)
  410.  
  411.  
  412. def abspath(path):
  413.     '''Return the absolute version of a path'''
  414.     if not isabs(path):
  415.         path = join(os.getcwd(), path)
  416.     
  417.     return normpath(path)
  418.  
  419. realpath = abspath
  420. supports_unicode_filenames = False
  421.